package com.yanqu.road.server.cmd.wingroom.message;

import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.wingroom.WingRoomMessageModule;
import com.yanqu.road.server.manger.wingroom.WingRoomMessageMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

/**
 * @author ：zfr
 * @date ：2022/3/18 14:46
 */
@Cmd(code = GameProtocol.S_WING_ROOM_GIVE_MESSAGE_LIKE, desc = "点赞")
public class GiveMessageLikeCmd extends AbstractCommand {
    private long ownerServerId;
    private long ownerUserId;
    private WingRoomProto.GiveMessageLikeReqMsg reqMsg;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = WingRoomProto.GiveMessageLikeReqMsg.parseFrom(packet.getBytes());
        ownerServerId = reqMsg.getOwnerServerId();
        ownerUserId = reqMsg.getOwnerUserId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        WingRoomProto.GiveMessageLikeRespMsg.Builder respMsg = WingRoomProto.GiveMessageLikeRespMsg.newBuilder();
        ret = player.getModule(WingRoomMessageModule.class).checkGiveLike(GameServer.getInstance().getServerId(), ownerUserId);
        if (ret != 0) {
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_WING_ROOM_GIVE_MESSAGE_LIKE, respMsg);
            return;
        }
        //本服
        if (ownerServerId == GameServer.getInstance().getServerId()) {
            respMsg = WingRoomMessageMgr.giveMessageLike(ownerUserId, player.getUserId());
            if (respMsg.getRet() == 0) {
                player.getModule(WingRoomMessageModule.class).addGiveLikeData(GameServer.getInstance().getServerId(), ownerUserId);
            }
            player.sendPacket(ClientProtocol.U_WING_ROOM_GIVE_MESSAGE_LIKE, respMsg);
            return;
        }
        //跨服
        //预先记录点赞记录
        player.getModule(WingRoomMessageModule.class).addPreRecordGiveLikeData(GameServer.getInstance().getServerId(), ownerUserId);
        player.sendPacket(CrossProtocol.C_WING_ROOM_GIVE_LIKE, reqMsg.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
